package glazedlists.chapter7;

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.ThresholdList;
import ca.odell.glazedlists.matchers.MatcherEditor;
import ca.odell.glazedlists.swing.AdvancedTableModel;
import ca.odell.glazedlists.swing.GlazedListsSwing;
import ca.odell.glazedlists.swing.TableComparatorChooser;
import ca.odell.glazedlists.swing.TextComponentMatcherEditor;
import ca.odell.issuezilla.Issue;
import glazedlists.chapter2.IssueComparator;
import glazedlists.chapter2.IssueTableFormat;
import glazedlists.chapter3.IssueTextFilterator;
import glazedlists.chapter5.UsersSelect;
import glazedlists.chapter6.IssuesLoader;

import javax.swing.*;
import java.awt.*;
import java.util.Hashtable;

public class IssuesBrowser7 {

    // reads issues from a stream and populates the issues event list
    private IssuesLoader issuesLoader = new IssuesLoader();
    private EventList<Issue> issueEventList = issuesLoader.getIssues();

    public IssuesBrowser7() {
        issuesLoader.load();
    }

    public void display() {
        JList<String> userSelectList;
        JTextField filterEdit;
        ThresholdList<Issue> priorityFilteredIssues;
        JScrollPane issueTableScrollPane;
        issueEventList.getReadWriteLock().readLock().lock();
        try {
            UsersSelect usersSelect = new UsersSelect(issueEventList);
            userSelectList = usersSelect.getJList();
            FilterList<Issue> userFilteredIssues = new FilterList<>(issueEventList, usersSelect);
            filterEdit = new JTextField(10);
            IssueTextFilterator filterator = new IssueTextFilterator();
            MatcherEditor<Issue> matcherEditor = new TextComponentMatcherEditor<Issue>(filterEdit, filterator);
            FilterList<Issue> textFilteredIssues = new FilterList<>(userFilteredIssues, matcherEditor);

            IssuePriorityThresholdEvaluator evaluator = new IssuePriorityThresholdEvaluator();
            priorityFilteredIssues = new ThresholdList<Issue>(textFilteredIssues, evaluator);

            SortedList<Issue> sortedIssues = new SortedList<>(priorityFilteredIssues, new IssueComparator());

            AdvancedTableModel<Issue> tableModel = GlazedListsSwing.eventTableModelWithThreadProxyList(sortedIssues,
                    new IssueTableFormat());
            JTable issueJTable = new JTable(tableModel);
            TableComparatorChooser.install(issueJTable, sortedIssues, TableComparatorChooser.MULTIPLE_COLUMN_MOUSE);
            issueTableScrollPane = new JScrollPane(issueJTable);
        } finally {
            issueEventList.getReadWriteLock().readLock().unlock();
        }

        BoundedRangeModel priorityFilterRangeModel = GlazedListsSwing.lowerRangeModel(priorityFilteredIssues);
        priorityFilterRangeModel.setRangeProperties(1, 0, 1,5, false);
        JSlider priorityFilterSlider = new JSlider(priorityFilterRangeModel);
        Hashtable<Integer, JLabel> priorityFilterSliderLabels = new Hashtable<>();
        priorityFilterSliderLabels.put(new Integer(1), new JLabel("Low"));
        priorityFilterSliderLabels.put(new Integer(5), new JLabel("High"));
        priorityFilterSlider.setLabelTable(priorityFilterSliderLabels);
        priorityFilterSlider.setMajorTickSpacing(1);
        priorityFilterSlider.setSnapToTicks(true);
        priorityFilterSlider.setPaintLabels(true);
        priorityFilterSlider.setPaintTicks(true);

        JScrollPane usersListScrollPane = new JScrollPane(userSelectList);

        JPanel panel = new JPanel(new GridBagLayout());
        panel.add(new JLabel("Filter: "),           new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 0, 5), 0, 0));
        panel.add(filterEdit, new GridBagConstraints(0, 1, 1, 1, 0.15, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 5, 10, 5), 0, 0));
        panel.add(new JLabel("Minimum Priority: "), new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 0, 5), 0, 0));
        panel.add(priorityFilterSlider,             new GridBagConstraints(0, 3, 1, 1, 0.15, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 5, 10, 5), 0, 0));
        panel.add(new JLabel("Reported By: "),      new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 0, 5), 0, 0));
        panel.add(usersListScrollPane,              new GridBagConstraints(0, 5, 1, 1, 0.15, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 5, 5, 5), 0, 0));
        panel.add(issueTableScrollPane,            new GridBagConstraints(1, 0, 1, 6, 0.85, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(540, 480);
        frame.getContentPane().add(panel);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            IssuesBrowser7 issuesBrowser = new IssuesBrowser7();
            issuesBrowser.display();
        });
    }
}
